﻿using MyMusicPlay._4._0.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MyMusicPlay._4._0
{
    /// <summary>
    /// TempWindows.xaml 的交互逻辑
    /// </summary>
    public partial class DisplayMusicWindow : Window
    {

        private Media _media;
        public DisplayMusicWindow()
        {
            InitializeComponent();

            this.Topmost = true;

            _media = PlayApplication.GetModule<Media>();

            _media.UpdateEvent((media) => { media.MediaOpened += DisplayMusic_MediaElement_MediaOpened; });

            DisplayMusic_MediaElement_MediaOpened(null, null);
        }

        public void DisplayMusic_MediaElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            var music = _media.GetCurMusic;
            if (music is null)
                return;
            if (string.IsNullOrEmpty(music.Singer))
                MusicDisplay_TextBlock.Text = $"{music.Name}";
            else
                MusicDisplay_TextBlock.Text = $"{music.Name} - {music.Singer}";
        }

        private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            WindowsManage.Remove<DisplayMusicWindow>();
        }




        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

        [DllImport("user32.dll")]
        private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        public static void SetWindowToForegroundWithAttachThreadInput(Window window)
        {
            var interopHelper = new WindowInteropHelper(window);
            var thisWindowThreadId = GetWindowThreadProcessId(interopHelper.Handle, IntPtr.Zero);
            var currentForegroundWindow = GetForegroundWindow();
            var currentForegroundWindowThreadId = GetWindowThreadProcessId(currentForegroundWindow, IntPtr.Zero);

            AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);

            window.Show();
            window.Activate();
            // 去掉和其他线程的输入链接
            AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false);
            // 用于踢掉其他的在上层的窗口
            window.Topmost = true;
            window.Topmost = false;
        }


    }
}
